import processing.opengl.*;
import processing.core.*;

//import fullscreen.*; 

public class multitouch_window_experiment extends PApplet {
  
  

//FullScreen fs; 

int xTotal = 100;
int xUnit = 8;

int yTotal = 100;
int yUnit = 6;
int total = xTotal * yTotal;
int xWidth = 800, yHeight = 600;

Obj[] obj =  new Obj[total];
Meander worm = new Meander();
boolean clear = true;

void setup() {
  size(800,600,P3D);
 hint( ENABLE_OPENGL_4X_SMOOTH );
  //colorMode( RGB, 100.0 );
  //noBackground();

  // Create the fullscreen object
  //fs = new FullScreen(this); 
  
  // enter fullscreen mode
  //fs.enter(); 
  
  
  for (int i=0; i<xTotal; i++){
    for (int k=0; k<yTotal; k++){
      obj[(k*yTotal) + i] = new Obj(i * xUnit, k * yUnit);
      obj[(k*yTotal) + i].index = (k*yTotal) + i;
    }
  }
}
 
void draw() {

  drawState();
  //System.arraycopy(bg.pixels, 0, pixels, 0, width*height); 
  for (int i=0; i<total; i++){
    obj[i].objRotate();
    obj[i].objPosition();
    obj[i].objDraw();
  }
  worm.move();
} 

void drawState(){
 

  if (clear){
    fill(0);
    noStroke();
    rect(0,0,width, height);
  } else {
    fill(0);
    noStroke();
    rect(0,0,width, height);
    drawLines();
  }
}

void keyReleased () {
  if (key == ' ' && clear) { 
    clear = false;
  } else if (key == ' ' && !clear) {
    clear = true;
  }
}


class Obj {
  float R = .2f;
  float F = .7f;
  float index;
  float xDelta, yDelta;
  float xp, yp, xNew, yNew;
  float radian;
  float xInit, yInit;
  float xPos;
  float yPos;
  float ax, ay;
  float distance, radius;
  float angle;
  float counter = 0;
  float incr = .2f;
  float tempRadius;
  int[] aPixels; 
  
  Obj (float xi, float yi) {
    xInit = xi;
    yInit = yi;
    xPos = xInit;
    yPos = yInit;

  }
  
  void objRotate () {
    xDelta = worm.xPos - xInit;
    yDelta = worm.yPos - yInit;
    radian = atan2(yDelta,xDelta) * 180/PI;
  }
  
  void objPosition () {
    distance = sqrt(sq(xDelta) + sq(yDelta));
    angle = (radian * (PI/180) - 180) + 45;
    xp = radius * cos(angle);
    yp = radius * sin(angle);
    xNew = xInit + xp;
    yNew = yInit + yp;
    
    tempRadius = (sin(counter) * 50) + 100;
    counter = counter + incr;
    
    if (mousePressed){
      if (distance < 150){
        radius = 150 - distance;
      } else {
        radius = 0;
      }
    } else {
      if (distance < tempRadius){
        radius = (tempRadius - distance)/5;
      } else {
        radius = 0;
      }
    }
  
    ax = (ax + (xPos - xNew) * R) * F;
    ay = (ay + (yPos - yNew) * R) * F;
    xPos = xPos - ax;
    yPos = yPos - ay;
  }
  
  void objDraw(){
   stroke(255 - ((255/tempRadius) * radius));
   fill(150,205,174);
   rect(xPos, yPos,8,6);
  }
}

void drawLines(){
  for (int i=0; i<xTotal-1; i++){
    noFill();
    beginShape();
    for (int k=0; k<yTotal-1; k++){
      stroke(255);
      curveVertex(obj[(i*xTotal) + k].xPos, obj[(i*xTotal) + k].yPos);
    }
    endShape();
  }
  
  for (int k=0; k<yTotal-1; k++){
    noFill();
    beginShape();
    for (int i=0; i<xTotal-1; i++){
      stroke(255);
      curveVertex(obj[(i*xTotal) + k].xPos, obj[(i*xTotal) + k].yPos);
    }
    endShape();
  }
}


class Meander {
  float border = 50;
  float left = border, right = width - border, bottom = border, top = height - border;
  float xMax = 10, yMax = 10;
  float xPos = 300, yPos = 300;
  float xLimit, yLimit, xSpeed, ySpeed, xDelta, yDelta;
  
  Meander (){}
  
  void move(){
    xPos = xPos - (xPos - mouseX) * .2f;
    yPos = yPos - (yPos - mouseY) * .2f;
  }
}    

}
